/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: CardListAdapter
 * Author: Clark
 * Date: 2020/10/15 18:17
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
package sz.szsmk.citizencard.ui.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.response.QueryUserCardRes;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * @ClassName: CardListAdapter
 * @Description:
 * @Author: Clark
 * @Date: 2020/10/15 18:17
 */
public class CardListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context mContext;
    private List<QueryUserCardRes.DataBean> mList = new ArrayList<>();
    private Drawable upDrawable;
    private Drawable downDrawable;
    private boolean trafficSpread = false;
    private boolean citizenSpread = false;
    private int trafficSize;
    private int citizenSize;
    private int isFirst = 0;

    private List<QueryUserCardRes.DataBean> listAll = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listTraffic = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listCitizen = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listTrafficAdd = new ArrayList<>();//2
    private List<QueryUserCardRes.DataBean> listTrafficAdd3 = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listTrafficAdd4 = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listCitizenAdd2 = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listCitizenAdd3 = new ArrayList<>();
    private List<QueryUserCardRes.DataBean> listCitizenAdd4 = new ArrayList<>();

    public CardListAdapter(Context context, List<QueryUserCardRes.DataBean> list, int trafficSize, int citizenSize, boolean trafficSpread, boolean citizenSpread, int isFirst) {
        this.mContext = context;
        if (trafficSpread == true && citizenSpread == true && isFirst == 0) {
            //数据初始化
            listAll.clear();
            listAll.addAll(list);
            this.trafficSize = trafficSize;
            this.citizenSize = citizenSize;
            for (int i = 0; i < trafficSize; i++) {
                listTraffic.add(list.get(i));
                if (i >= 2) {
                    listTrafficAdd.add(list.get(i));
                }
                if (i >= 3) {
                    listTrafficAdd3.add(list.get(i));
                }
                if (i >= 4) {
                    listTrafficAdd4.add(list.get(i));
                }
            }
            for (int m = trafficSize; m < list.size(); m++) {
                listCitizen.add(list.get(m));
            }
            for (int i = 0; i < listCitizen.size(); i++) {
                if (i >= 2) {
                    listCitizenAdd2.add(listCitizen.get(i));
                }
                if (i >= 3) {
                    listCitizenAdd3.add(listCitizen.get(i));
                }
                if (i >= 4) {
                    listCitizenAdd4.add(listCitizen.get(i));
                }
            }
//                if (m - trafficSize > 2) {
//                    listCitizenAdd2.add(list.get(m));
//                }
//                if (m - trafficSize > 3) {
//                    listCitizenAdd3.add(list.get(m));
//                }
//                if (m - trafficSize > 4) {
//                    listCitizenAdd4.add(list.get(m));
//                }


        }
        this.mList.clear();
        this.mList.addAll(list);
        this.trafficSpread = trafficSpread;
        this.citizenSpread = citizenSpread;
        if ((trafficSize == 0) && citizenSize > 4) {
            //最初的数据判断
            if (trafficSpread && citizenSpread && isFirst == 0) {//进来 收起列表，并且可以点击展开

                this.trafficSpread = false;
                this.citizenSpread = false;
                this.isFirst = 1;
                mList.removeAll(listTrafficAdd);
                mList.removeAll(listCitizenAdd4);
                notifyDataSetChanged();
            }
        }
        if ((trafficSize == 1) && citizenSize > 3) {
            //最初的数据判断
            if (trafficSpread && citizenSpread && isFirst == 0) {//进来 收起列表，并且可以点击展开

                this.trafficSpread = false;
                this.citizenSpread = false;
                this.isFirst = 1;
                mList.removeAll(listTrafficAdd);
                mList.removeAll(listCitizenAdd3);
                notifyDataSetChanged();
            }
        }
        if (trafficSize == 2 && citizenSize > 2) {
            //最初的数据判断
            if (trafficSpread && citizenSpread && isFirst == 0) {//进来 收起列表，并且可以点击展开

                this.trafficSpread = false;
                this.citizenSpread = false;
                this.isFirst = 1;
                mList.removeAll(listTrafficAdd);
                mList.removeAll(listCitizenAdd2);
                notifyDataSetChanged();
            }
        }
        if ((trafficSize > 2 && citizenSize > 2)) {
            //最初的数据判断
            if (trafficSpread && citizenSpread && isFirst == 0) {//进来 收起列表，并且可以点击展开

                this.trafficSpread = false;
                this.citizenSpread = false;
                this.isFirst = 1;
                mList.removeAll(listTrafficAdd);
                mList.removeAll(listCitizenAdd2);
                notifyDataSetChanged();
            }
        }

        if ((trafficSize > 2 && citizenSize == 2)) {
            if (trafficSpread && citizenSpread && isFirst == 0) {//进来 收起列表，并且可以点击展开

                this.trafficSpread = false;
                this.citizenSpread = false;
                this.isFirst = 1;
                mList.removeAll(listTrafficAdd);
                notifyDataSetChanged();
            }
        }
        if ((trafficSize > 3 && citizenSize == 1)) {
            if (trafficSpread && citizenSpread && isFirst == 0) {//进来 收起列表，并且可以点击展开

                this.trafficSpread = false;
                this.citizenSpread = false;
                this.isFirst = 1;
                mList.removeAll(listTrafficAdd3);
                notifyDataSetChanged();
            }
        }
        if ((trafficSize > 4 && citizenSize == 0)) {
            if (trafficSpread && citizenSpread && isFirst == 0) {//进来 收起列表，并且可以点击展开

                this.trafficSpread = false;
                this.citizenSpread = false;
                this.isFirst = 1;
                mList.removeAll(listTrafficAdd4);
                notifyDataSetChanged();
            }
        }
    }

    @Override
    public CardListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CardListAdapter.ViewHolder viewHolder = null;
        if (viewHolder == null) {
//            if (viewType == 1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_card, parent, false);
            viewHolder = new CardListAdapter.ViewHolder(itemView);
//            } else {
//                View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_card_more, parent, false);
//                viewHolder = new CardListAdapter.ViewHolder(itemView);
//            }
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        //不同的位置进行处理
//        if (getItemViewType(position) == 1) {
        String type = mList.get(position).getCardDetailInfolist().get(0).getCardTypeName();
        ((ViewHolder) holder).Type.setText(type);
        ((ViewHolder) holder).label.setText(String.format(mContext.getResources().getString(R.string.card_owner),mList.get(position).getCardDetailInfolist().get(0).getCardNickName(),
                mList.get(position).getCardDetailInfolist().get(0).getCustName()));
        ((ViewHolder) holder).seriesNum.setText(mList.get(position).getBindCardNo());
        TextView textView = ((ViewHolder) holder).cardMore;
        if (type.contains("江苏交通")) {
            ((ViewHolder) holder).relativeLayout.setBackgroundColor(mContext.getResources().getColor(R.color.color_e8f4fe));
//            ((ViewHolder) holder).relativeLayout.setBackgroundColor(mContext.getResources().getColor(R.color.color_fef5e8));
        }
        if (type.contains("江苏省交通")) {
            ((ViewHolder) holder).relativeLayout.setBackgroundColor(mContext.getResources().getColor(R.color.color_e8f4fe));
        }
        if (type.contains("B卡")) {
            ((ViewHolder) holder).relativeLayout.setBackgroundColor(mContext.getResources().getColor(R.color.color_e8e9fe));
        }
        if (type.contains("社会保障")) {
            ((ViewHolder) holder).relativeLayout.setBackgroundColor(mContext.getResources().getColor(R.color.color_e8edfe));
        }

        ((ViewHolder) holder).relativeLayout.setOnClickListener(v -> {

            Log.d("clark", "jump activity");
            Bundle bundle = new Bundle();
            bundle.putSerializable("cardData", mList.get(position));
            JumpUtils.jumptoNatvie(mContext, "CitizenCardActivity", 0, bundle);

        });
        ((ViewHolder) holder).cardMore.setVisibility(View.GONE);
        //打开数据最初的界面
        if (trafficSize == 0 && citizenSize > 4) {
            //最初的数据判断
            if (!trafficSpread && citizenSpread && isFirst == 1) {
                if (position == mList.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listCitizenAdd4);
                            citizenSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
            }
            if (!trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == 3) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        //点击展开之后，可以收起
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(listCitizenAdd4);
                            citizenSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
        }
        if (trafficSize == 1 && citizenSize > 3) {

            if (!trafficSpread && citizenSpread && isFirst == 1) {
                if (position == mList.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listCitizenAdd3);
                            citizenSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
            }
            if (!trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == 3) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        //点击展开之后，可以收起
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(listCitizenAdd3);
                            citizenSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
        }
        if (trafficSize == 2 && citizenSize > 2) {
            if (!trafficSpread && citizenSpread && isFirst == 1) {
                if (position == mList.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listCitizenAdd2);
                            citizenSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
            }
            if (!trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == 3) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        setArrow(textView, "展开");
                        //点击展开之后，可以收起
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(listCitizenAdd2);
                            citizenSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
        }
        if (trafficSize > 2 && citizenSize > 2 ) {
            if (trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == trafficSize - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    Log.d("clark", "show data position1==" + position);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listTrafficAdd);
                            trafficSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
                if (position == mList.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    Log.d("clark", "show data position2==" + position);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(listTraffic.size() + 2, listCitizenAdd2);
                            citizenSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
            if (!trafficSpread && citizenSpread && isFirst == 1) {
                if (position == 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    Log.d("clark", "show data position3==" + position);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(2, listTrafficAdd);
                            trafficSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
                if (position == mList.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    Log.d("clark", "show data position4==" + position);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listCitizenAdd2);
                            citizenSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
            if (trafficSpread && citizenSpread && isFirst == 1) {
                if (position == listTraffic.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    Log.d("clark", "show data position==5" + position);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listTrafficAdd);
                            trafficSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
                if (position == mList.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    Log.d("clark", "show data position=6=" + position);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listCitizenAdd2);
                            citizenSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
            if (!trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == 1) {
                    Log.d("clark", "position==" + position);
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "展开");
                    Log.d("clark", "show data position=7=" + position);
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(2, listTrafficAdd);
                            trafficSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
                if (position == 3) {
                    Log.d("clark", "position==" + position);
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    Log.d("clark", "show data position=8=" + position);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(4, listCitizenAdd2);
                            citizenSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
        }

        if (trafficSize > 2 && citizenSize == 2) {
            //最初的数据判断
            if (trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == listTraffic.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listTrafficAdd);
                            trafficSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
            }
            if (!trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        //点击展开之后，可以收起
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(2, listTrafficAdd);
                            trafficSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
        }
        if (trafficSize > 3 && citizenSize == 1) {
            //最初的数据判断
            if (trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == listTraffic.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listTrafficAdd3);
                            trafficSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
            }
            if (!trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == 2) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        //点击展开之后，可以收起
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(3, listTrafficAdd3);
                            trafficSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
        }
        if (trafficSize > 4 && citizenSize == 0) {
            //最初的数据判断
            if (trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == listTraffic.size() - 1) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "收起");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("收起")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
                            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
                            mList.removeAll(listTrafficAdd4);
                            trafficSpread = false;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }
            }
            if (!trafficSpread && !citizenSpread && isFirst == 1) {
                if (position == 3) {
                    ((ViewHolder) holder).cardMore.setVisibility(View.VISIBLE);
                    setArrow(textView, "展开");
                    ((ViewHolder) holder).cardMore.setOnClickListener(v -> {
                        //点击展开之后，可以收起
                        if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
                            ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
                            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
                            ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
                            mList.addAll(4, listTrafficAdd4);
                            trafficSpread = true;
                            isFirst = 1;
                            notifyDataSetChanged();
                        }
                    });
                }

            }
        }
//        ((ViewHolder) holder).itemLayout.setOnClickListener(view -> {
//            if (((ViewHolder) holder).cardMore.getText().toString().contains("展开")) {
//                ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_close));
//                upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
//                ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, upDrawable, null);
//            } else {
//                ((ViewHolder) holder).cardMore.setText(mContext.getResources().getString(R.string.card_more));
//                downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
//                ((ViewHolder) holder).cardMore.setCompoundDrawables(null, null, downDrawable, null);
//            }
//        });


    }

    @Override
    public int getItemViewType(int position) {//返回item的格式
        return R.layout.item_card;
//        if (mList.get(position).getaCardNo().contains("-1")) {
//            return 0;
//        } else {
//            return 1;
//        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    /**
     * 判断position对应的Item是否是组的第一项a
     *
     * @param position
     * @return
     */
    public boolean isItemHeader(int position) {
        if(position>=mList.size()){
            position=mList.size()-1;
        }
        if (position == 0) {
            return true;
        } else {
            String lastGroupName = mList.get(position - 1).getCardDetailInfolist().get(0).getCardTypeName();
            String currentGroupName = mList.get(position).getCardDetailInfolist().get(0).getCardTypeName();
            //判断上一个数据的组别和下一个数据的组别是否一致，如果不一致则是不同组，也就是为第一项（头部）
            if (lastGroupName.contains("交通") && currentGroupName.contains("交通") || lastGroupName.contains("市民") && currentGroupName.contains("市民")) {
                return false;
            } else {
                return true;
            }
        }
    }

    /**
     * 获取position对应的Item组名
     *
     * @param position
     * @return
     */
    public String getGroupName(int position) {
        String type = "交通卡";
        if(position==0)
        if (mList.get(0).getCardDetailInfolist().get(0).getCardTypeName().contains("交通"))
            type = "交通卡";
        if (mList.get(0).getCardDetailInfolist().get(0).getCardTypeName().contains("市民"))
            type = "市民卡";

        if (position >= 1) {
            if (mList.get(position - 1).getCardDetailInfolist().get(0).getCardTypeName().contains("交通"))
                type = "交通卡";
            if (mList.get(position - 1).getCardDetailInfolist().get(0).getCardTypeName().contains("市民"))
                type = "市民卡";
        }
        return type;
    }

    public void setViewData(List<QueryUserCardRes.DataBean> listCardData) {
        mList.clear();
        mList.addAll(listCardData);
    }


    /**
     * 自定义ViewHolder
     */
    class ViewHolder extends RecyclerView.ViewHolder {

        TextView Type;
        TextView label;
        TextView seriesNum;
        RelativeLayout relativeLayout;
        LinearLayout itemLayout;
        TextView cardMore;

        public ViewHolder(View itemView) {
            super(itemView);
            Type = itemView.findViewById(R.id.text_label_traffic);
            label = itemView.findViewById(R.id.text_label_traffic_en);
            seriesNum = itemView.findViewById(R.id.iv_traffic_cardnum);
            relativeLayout = itemView.findViewById(R.id.rl_traffic_layout);
            itemLayout = itemView.findViewById(R.id.card_item_layout);
            cardMore = itemView.findViewById(R.id.card_more);
        }
    }

    class MoreViewHolder extends RecyclerView.ViewHolder {

        TextView more;

        public MoreViewHolder(View itemView) {
            super(itemView);
            more = itemView.findViewById(R.id.card_more);
        }
    }

    public void setArrow(TextView v, String s) {
        if (s.contains("展开")) {
            v.setText(mContext.getResources().getString(R.string.card_more));
            downDrawable.setBounds(0, 0, downDrawable.getIntrinsicWidth(), downDrawable.getIntrinsicHeight());
            v.setCompoundDrawables(null, null, downDrawable, null);
        } else if (s.contains("收起")) {
            v.setText(mContext.getResources().getString(R.string.card_close));
            upDrawable.setBounds(0, 0, upDrawable.getIntrinsicWidth(), upDrawable.getIntrinsicHeight());
            v.setCompoundDrawables(null, null, upDrawable, null);
        }


    }
}
